<?php
if(file_exists('../includes/include.php')){
require_once('../includes/include.php');
}
class install{
	public $autoload;
	public $_con;
	public $_config;
	public function __construct(){
		$this->autoload= new autoload();
		$this->connectDb();
		$this->createTables();
	}
	public function connectDb(){
		$configFile= simplexml_load_file($this->autoload->getConfig());
		$this->_config = $configFile;
		$host =(string) $configFile->system->host;
		$user =(string) $configFile->system->user;
		$pwd =(string) $configFile->system->password;
		$database =(string) $configFile->system->database;
		$con = mysql_connect($host,$user,$pwd);
		$this->_con=$con;
		$db = $this->selectDb($database);
		if(!$db){
			$this->createDb($database);
		}
		$db = $this->selectDb($database);
	}
	public function selectDb($database){
		$db = mysql_select_db($database,$this->_con);
		return $db;
	}
	public function createDb($database){
		$sql ="CREATE DATABASE `$database` DEFAULT CHARSET=utf8";
		$this->query($sql);
	}
	public function query($sql){
		return mysql_query($sql,$this->_con);
	}
	public function createTables(){
		$tableFiles = (string) $this->_config->system->tables;
		$tableFiles = $this->autoload->getBaseDir()."/".$tableFiles;
		$tables = file_get_contents($tableFiles);
		$sqlArray = explode(";",$tables);
		foreach($sqlArray as $key=>$sql){
			$sql = trim($sql);
			if($sql){
				$query = $this->query($sql,$this->_con);
			}
		}
	}
}
$install = new install();
?>